﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Configuration;
using System.Data.OracleClient;
using System.Data;

public partial class Search : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
     
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string connStr = WebConfigurationManager.ConnectionStrings["ConnectionStringForOracle"].ConnectionString;
        string commStr = "";

        string jh = tb_jh.Text;
        string begin_date = tb_begin_date.Text;
        string end_date = tb_end_date.Text;
        string cslb = DropDownList1.SelectedValue;

        //两个日期textbox的输入必须同时为空，或者同时不为空
        if (begin_date == "" && end_date != "")
        {
            lb_message.Text = "请输入结束日期!";
            return;
        }
        else if (begin_date != "" && end_date == "")
        {
            lb_message.Text = "请输入开始日期!";
            return;
        }

        //I think i'm right this time.
        commStr = "select * from bc_cscgjl where jh='" + jh + "' and wgrq >=to_date('" + begin_date + "','yyyy-mm-dd') and wgrq <=to_date('" + end_date + "','yyyy-mm-dd') and cslb like '" + cslb + "'";
        if (jh == "")
        {
            int lIndex = commStr.IndexOf("jh");
            int rIndex = commStr.IndexOf("wgrq");
            string sub = commStr.Substring(lIndex,(rIndex - lIndex));
            commStr = commStr.Replace(sub,string.Empty);
        }
        if (begin_date == "")
        {
            //删除从wgrq到cslb前的字符
            int lIndex = commStr.IndexOf("wgrq");
            int rIndex = commStr.IndexOf("cslb");
            string sub = commStr.Substring(lIndex, (rIndex - lIndex));
            commStr = commStr.Replace(sub, string.Empty);
        }
        if (cslb == "全部")
        {
            //delete cslb to the end of string.
            commStr = commStr.Remove(commStr.IndexOf("and cslb"));
        }
        if (jh == "" && begin_date == "" && cslb == "")
        {
            //delete where.
            commStr = commStr.Replace("where",string.Empty);
        }

        OracleConnection conn = new OracleConnection(connStr);
        OracleCommand comm = new OracleCommand(commStr, conn);

        try
        {
            conn.Open();
            OracleDataReader reader = comm.ExecuteReader(CommandBehavior.CloseConnection);
            GridView1.DataSource = reader;
            GridView1.DataBind();
        }
        catch
        {
            lb_message.Text = "请确认您输入了正确的井号！";
            throw;
        }
        finally
        {
            conn.Close();
        }

        //setFields();

    }
    private void setFields()
    {
        string connStr = WebConfigurationManager.ConnectionStrings["ConnectionStringForOracle"].ConnectionString;
        string commStr = "select a.comments from user_col_comments a,user_tab_columns b where a.table_name = 'BC_CSCGJL' and b.table_name = 'BC_CSCGJL' and a.COLUMN_NAME = b.COLUMN_NAME order by b.column_id";
        OracleConnection conn = new OracleConnection(connStr);
        OracleCommand comm = new OracleCommand(commStr, conn);      
        
        try
        {
            conn.Open();
            OracleDataReader reader = comm.ExecuteReader(CommandBehavior.CloseConnection);

            if (reader.HasRows)
            {
                int i = 0;
                while (reader.Read())
                {
                    GridView1.HeaderRow.Cells[i].Text = reader[0].ToString();
                    i++;
                }
            }
        }
        finally
        {
            conn.Close();
        }
    }
}